// TODO - use image palette array, don't create a new palette

/* This module controls the color cycle of the water. To create a water cycle
 * in the image, do this:
 *
 *   1. Initialize the water object.
 *   2. Use a given color index to paint the water area.
 *   3. Use `water_replace` to replace that color index by water color indexes.
 *   4. In each frame, call `water_cycle`. */

#include "water.h"

#include <stdlib.h>

#include "global.h"
#include "algorithm.h"

/* Initialize the water object. The parameters are:
 *   - dark: the darkest water color
 *   - light: the brightest water color
 *   - palete_pos: the initial palette position that will be used for the
 *                 water. Beware that PAL_SIZE palette positions will be
 *                 overwritten. */
Water* water_init(SDL_Surface* sf, SDL_Color* color, SDL_Color dark, SDL_Color light, 
		int palette_pos)
{
	Water *water = malloc(sizeof(Water));

	// create colors
	SDL_Color temp[256];
	create_gradient(temp, dark, light, 0, 5);

	// feed water object
	water->color = color;
	water->color[palette_pos+0] = temp[0];
	water->color[palette_pos+1] = temp[0];
	water->color[palette_pos+2] = temp[0];
	water->color[palette_pos+3] = temp[0];
	water->color[palette_pos+4] = temp[0];
	water->color[palette_pos+5] = temp[1];
	water->color[palette_pos+5] = temp[1];
	water->color[palette_pos+6] = temp[2];
	water->color[palette_pos+7] = temp[3];
	water->color[palette_pos+8] = temp[4];
	water->color[palette_pos+9] = temp[3];
	water->color[palette_pos+10] = temp[2];
	water->color[palette_pos+11] = temp[1];
	water->surface = sf;
	water->palette_pos = palette_pos;
	water->frame = 0;

	water_set_palette(water, sf);

	return water;
}


void water_set_palette(Water* water, SDL_Surface* sf)
{
	SDL_SetPalette(sf, SDL_LOGPAL|SDL_PHYSPAL, water->color, 
			water->palette_pos, PAL_SIZE);
}


// Draw water in all pixels in `sf`, where the color index is `replace`.
void water_replace(Water *w, SDL_Surface *sf, int replace)
{
	if(SDL_MUSTLOCK(sf))
		if(SDL_LockSurface(sf) == -1)
			ui_error("Could not lock surface.");

	Uint8* pixels = ((Uint8*) sf->pixels);
	srand(0);

	int x, y;
	int cx = 0;
	float cy = 0.0;
	for(y=0; y<sf->h; y++)
	{
		for(x=0; x<sf->w; x++)
		{
			int pixpos = x + (y * sf->pitch);
			if(pixels[pixpos] == replace)
			{
				int c = cx + cy;
				int r = rand() % 12;
				if(r == 0)
					c -= 3;
				else if(r <= 2)
					c -= 2;
				else if(r <= 4)
					c -= 1;
				else if(r == 11)
					c += 3;
				else if(r >= 9)
					c += 2;
				else if(r >= 7)
					c += 1;
				pixels[pixpos] = (c % PAL_SIZE) + w->palette_pos;
			}
			++cx;
		}
		int r = rand() % 30;
		cx = ((int)y % 3) * 4; // I don't know why is that, but it works
		cy += ((float)r / 10.0f);
	}

	if(SDL_MUSTLOCK(sf))
		SDL_UnlockSurface(sf);
}


// Cycle the water. This function will be called once for each frame.
void water_cycle(Water *w)
{
	if(w->frame % 6 == 0)
		rotate_palette(w->color, w->palette_pos, w->palette_pos + PAL_SIZE);

	w->frame++;
}


// Free the water palette
void free_water(Water *w)
{
	if(w)
	{
		free(w); // TODO
	}
}
